<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>使用RabbitMQ实现未支付订单在30分钟后自动过期 | 一只特立独行的猪^(*￣(oo)￣)^</title>
    <meta name="generator" content="VuePress 1.8.0">
    
    <meta name="description" content="一只特立独行的猪的分享">
    
    <link rel="preload" href="/press-blog/assets/css/0.styles.41d49717.css" as="style"><link rel="preload" href="/press-blog/assets/js/app.d4168a11.js" as="script"><link rel="preload" href="/press-blog/assets/js/2.8d6ea248.js" as="script"><link rel="preload" href="/press-blog/assets/js/10.f7fbfdbe.js" as="script"><link rel="prefetch" href="/press-blog/assets/js/3.a7b40a35.js"><link rel="prefetch" href="/press-blog/assets/js/4.77a548c8.js"><link rel="prefetch" href="/press-blog/assets/js/5.5fd3c55b.js"><link rel="prefetch" href="/press-blog/assets/js/6.706caab8.js"><link rel="prefetch" href="/press-blog/assets/js/7.74938af9.js"><link rel="prefetch" href="/press-blog/assets/js/8.780b8f13.js"><link rel="prefetch" href="/press-blog/assets/js/9.b3c9fa86.js">
    <link rel="stylesheet" href="/press-blog/assets/css/0.styles.41d49717.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/press-blog/" class="home-link router-link-active"><!----> <span class="site-name">一只特立独行的猪^(*￣(oo)￣)^</span></a> <div class="links"><!----> <nav class="nav-links can-hide"><div class="nav-item"><a href="/press-blog/" class="nav-link router-link-active">
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="博文" class="dropdown-title"><span class="title">博文</span> <span class="arrow down"></span></button> <button type="button" aria-label="博文" class="mobile-dropdown-title"><span class="title">博文</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/press-blog/rabbitmq/delivery.html" class="nav-link">
  RabbitMQ
</a></li><li class="dropdown-item"><!----> <a href="/press-blog/multi-thread/" class="nav-link">
  多线程
</a></li><li class="dropdown-item"><!----> <a href="/press-blog/java/" class="nav-link">
  java
</a></li></ul></div></div><div class="nav-item"><a href="/press-blog/about/" class="nav-link">
  关于
</a></div><div class="nav-item"><a href="https://www.github.com/futaosmile" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/press-blog/" class="nav-link router-link-active">
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="博文" class="dropdown-title"><span class="title">博文</span> <span class="arrow down"></span></button> <button type="button" aria-label="博文" class="mobile-dropdown-title"><span class="title">博文</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/press-blog/rabbitmq/delivery.html" class="nav-link">
  RabbitMQ
</a></li><li class="dropdown-item"><!----> <a href="/press-blog/multi-thread/" class="nav-link">
  多线程
</a></li><li class="dropdown-item"><!----> <a href="/press-blog/java/" class="nav-link">
  java
</a></li></ul></div></div><div class="nav-item"><a href="/press-blog/about/" class="nav-link">
  关于
</a></div><div class="nav-item"><a href="https://www.github.com/futaosmile" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>RabbitMQ</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/press-blog/rabbitmq/delivery.html" class="sidebar-link">SpringBoot RabbitMQ实现消息可靠投递</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/press-blog/rabbitmq/delivery.html#一、实现思路" class="sidebar-link">一、实现思路</a></li><li class="sidebar-sub-header"><a href="/press-blog/rabbitmq/delivery.html#二、准备-框架搭建" class="sidebar-link">二、准备，框架搭建</a></li><li class="sidebar-sub-header"><a href="/press-blog/rabbitmq/delivery.html#三、编码" class="sidebar-link">三、编码</a></li><li class="sidebar-sub-header"><a href="/press-blog/rabbitmq/delivery.html#四、测试" class="sidebar-link">四、测试</a></li><li class="sidebar-sub-header"><a href="/press-blog/rabbitmq/delivery.html#next" class="sidebar-link"># Next</a></li></ul></li><li><a href="/press-blog/rabbitmq/dead-letter.html" class="sidebar-link">RabbitMQ死信队列在SpringBoot中的使用</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/press-blog/rabbitmq/expire.html" aria-current="page" class="active sidebar-link">使用RabbitMQ实现未支付订单在30分钟后自动过期</a><ul class="sidebar-sub-headers"></ul></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>核心功能</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>扩展</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>插件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>FAQ</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="使用rabbitmq实现未支付订单在30分钟后自动过期"><a href="#使用rabbitmq实现未支付订单在30分钟后自动过期" class="header-anchor">#</a> 使用RabbitMQ实现未支付订单在30分钟后自动过期</h1> <blockquote><p>延迟队列可以实现消息在投递到<code>Exchange</code>之后，经过一定的时间之后再投递到相应的<code>Queue</code>。再被消费者监听消费。 即：生产者投递的消息经过一段时间之后再被消费者消费。</p></blockquote> <ul><li>业务场景：订单在30分钟内还未支付则自动取消。</li></ul> <p><em><strong>该业务的其他实现方案:</strong></em></p> <ul><li>使用<code>Redis</code>，设置过期时间，监听过期事件。</li> <li></li></ul> <p>使用RabbitMQ的过期队列与死信队列，设置消息的存活时间，在设置的时间内未被消费，即会投递到死信队列，我们监听死信队列即可。可参考上一篇文章<a href="https://www.jianshu.com/p/be0361e222ac" target="_blank" rel="noopener noreferrer">RabbitMQ死信队列在SpringBoot中的使用<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>。</p> <p><strong>使用RabbitMQ延迟队列实现:</strong></p> <h3 id="安装延迟队列插件"><a href="#安装延迟队列插件" class="header-anchor">#</a> # 安装延迟队列插件:</h3> <ul><li>RabbitMQ插件列表: <a href="https://www.rabbitmq.com/community-plugins.html" target="_blank" rel="noopener noreferrer">https://www.rabbitmq.com/community-plugins.html<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li>延迟队列插件下载地址: https://github.com/rabbitmq/rabbitmq-delayed-message-exchange</li> <li>Mac下的插件路径为<code>/usr/local/Cellar/rabbitmq/3.7.15/plugins</code></li> <li>安装并启用</li></ul> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-fed49039701bef58.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="plugin"></p> <ul><li>重启RabbitMQ</li></ul> <h3 id="业务相关代码编写"><a href="#业务相关代码编写" class="header-anchor">#</a> # 业务相关代码编写</h3> <ul><li>订单实体(仅保留相关字段)</li></ul> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-f4f137bac0906074.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="OrderEntity"></p> <ul><li>订单状态枚举(仅保留相关状态)</li></ul> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-a20a2f4ab5d498da.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p> <ul><li>OrderMapper</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * @author futao
 * @date 2020/4/14.
 */</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">OrderMapper</span> <span class="token keyword">extends</span> <span class="token class-name">BaseMapper</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Order</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><ul><li>模拟下定的接口<code>OrderController</code> <em>为了简单起见，省略了Service层.</em></li></ul> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-e72fda49ac985fe4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="OrderController"></p> <h3 id="rabbitmq相关代码编写"><a href="#rabbitmq相关代码编写" class="header-anchor">#</a> # RabbitMQ相关代码编写</h3> <ul><li>配置文件</li></ul> <div class="language-yml line-numbers-mode"><pre class="language-yml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">rabbitmq</span><span class="token punctuation">:</span>
    <span class="token key atrule">host</span><span class="token punctuation">:</span> localhost
    <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">5672</span>
    <span class="token key atrule">username</span><span class="token punctuation">:</span> futao
    <span class="token key atrule">password</span><span class="token punctuation">:</span> <span class="token number">123456789</span>
    <span class="token key atrule">virtual-host</span><span class="token punctuation">:</span> delay<span class="token punctuation">-</span>vh
    <span class="token key atrule">connection-timeout</span><span class="token punctuation">:</span> <span class="token number">15000</span>
    <span class="token comment"># 发送确认</span>
    <span class="token key atrule">publisher-confirms</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
    <span class="token comment"># 路由失败回调</span>
    <span class="token key atrule">publisher-returns</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
    <span class="token key atrule">template</span><span class="token punctuation">:</span>
      <span class="token comment"># 必须设置成true 消息路由失败通知监听者，而不是将消息丢弃</span>
      <span class="token key atrule">mandatory</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
    <span class="token key atrule">listener</span><span class="token punctuation">:</span>
      <span class="token key atrule">simple</span><span class="token punctuation">:</span>
        <span class="token comment"># 每次从RabbitMQ获取的消息数量</span>
        <span class="token key atrule">prefetch</span><span class="token punctuation">:</span> <span class="token number">1</span>
        <span class="token key atrule">default-requeue-rejected</span><span class="token punctuation">:</span> <span class="token boolean important">false</span>
        <span class="token comment"># 每个队列启动的消费者数量</span>
        <span class="token key atrule">concurrency</span><span class="token punctuation">:</span> <span class="token number">1</span>
        <span class="token comment"># 每个队列最大的消费者数量</span>
        <span class="token key atrule">max-concurrency</span><span class="token punctuation">:</span> <span class="token number">1</span>
        <span class="token comment"># 手动签收ACK</span>
        <span class="token key atrule">acknowledge-mode</span><span class="token punctuation">:</span> manual

<span class="token key atrule">app</span><span class="token punctuation">:</span>
  <span class="token key atrule">rabbitmq</span><span class="token punctuation">:</span>
    <span class="token comment"># 延迟时长设置</span>
    <span class="token key atrule">delay</span><span class="token punctuation">:</span>
      <span class="token key atrule">order</span><span class="token punctuation">:</span> 10S
    <span class="token comment"># 队列定义</span>
    <span class="token key atrule">queue</span><span class="token punctuation">:</span>
      <span class="token key atrule">order</span><span class="token punctuation">:</span>
        <span class="token key atrule">delay</span><span class="token punctuation">:</span> order<span class="token punctuation">-</span>delay<span class="token punctuation">-</span>queue
    <span class="token comment"># 交换机定义</span>
    <span class="token key atrule">exchange</span><span class="token punctuation">:</span>
      <span class="token key atrule">order</span><span class="token punctuation">:</span>
        <span class="token key atrule">delay</span><span class="token punctuation">:</span> order<span class="token punctuation">-</span>delay<span class="token punctuation">-</span>exchange
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br></div></div><ul><li>延迟交换机，队列定义与绑定</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>
<span class="token comment">/**
 * 队列，交换机定义与绑定
 * 延迟队列插件`rabbitmq-delayed-message-exchange`下载地址 https://github.com/rabbitmq/rabbitmq-delayed-message-exchange
 *
 * @author futao
 * @date 2020/4/10.
 */</span>
<span class="token annotation punctuation">@Configuration</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Declare</span> <span class="token punctuation">{</span>

    <span class="token comment">/**
     * 订单队列 - 接收延迟投递的订单
     *
     * @param orderQueue 订单队列名称
     * @return
     */</span>
    <span class="token annotation punctuation">@Bean</span>
    <span class="token keyword">public</span> <span class="token class-name">Queue</span> <span class="token function">orderDelayQueue</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">&quot;${app.rabbitmq.queue.order.delay}&quot;</span><span class="token punctuation">)</span> <span class="token class-name">String</span> orderQueue<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">QueueBuilder</span>
                <span class="token punctuation">.</span><span class="token function">durable</span><span class="token punctuation">(</span>orderQueue<span class="token punctuation">)</span>
                <span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">/**
     * 订单交换机-延迟交换机 - 消息延迟一定时间之后再投递到绑定的队列
     *
     * @param orderExchange 订单延迟交换机
     * @return
     */</span>
    <span class="token annotation punctuation">@Bean</span>
    <span class="token keyword">public</span> <span class="token class-name">Exchange</span> <span class="token function">orderDelayExchange</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">&quot;${app.rabbitmq.exchange.order.delay}&quot;</span><span class="token punctuation">)</span> <span class="token class-name">String</span> orderExchange<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> args <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        args<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;x-delayed-type&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;topic&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">CustomExchange</span><span class="token punctuation">(</span>orderExchange<span class="token punctuation">,</span> <span class="token string">&quot;x-delayed-message&quot;</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">/**
     * 订单队列-交换机 绑定
     *
     * @param orderQueue         订单队列
     * @param orderDelayExchange 订单交换机
     * @return
     */</span>
    <span class="token annotation punctuation">@Bean</span>
    <span class="token keyword">public</span> <span class="token class-name">Binding</span> <span class="token function">orderBinding</span><span class="token punctuation">(</span><span class="token class-name">Queue</span> orderDelayQueue<span class="token punctuation">,</span> <span class="token class-name">Exchange</span> orderDelayExchange<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">BindingBuilder</span>
                <span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>orderDelayQueue<span class="token punctuation">)</span>
                <span class="token punctuation">.</span><span class="token keyword">to</span><span class="token punctuation">(</span>orderDelayExchange<span class="token punctuation">)</span>
                <span class="token punctuation">.</span><span class="token keyword">with</span><span class="token punctuation">(</span><span class="token string">&quot;order.delay.*&quot;</span><span class="token punctuation">)</span>
                <span class="token punctuation">.</span><span class="token function">noargs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br></div></div><p><em>可以看出队列就是普通的队列。重点在交换机的设定上。声明延迟交换机需要设置参数<code>x-delayed-type</code>，值为交换机类型，可以是<code>fanout</code>,<code>topic</code>,<code>direct</code>
。并且设置交换机的type为<code>x-delayed-message</code>。</em></p> <ul><li>定义完成后可以启动SpringBoot应用程序，在RabbitMQ管理后台查看Exchange和Queue。</li></ul> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-04ee5c3169037bc9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="delay-exchange"></p> <p><em>可以看到，除了默认的交换机，SpringBoot已经帮我们创建好了延迟交换机<code>order-delay-exchange</code>，并且此时messages delayed为0，因为我们还未向交换机投递消息。</em></p> <ul><li>可以继续查看交换机的路由类型与绑定的队列</li></ul> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-4aba4389c76ecbef.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="ExchangeDetail"></p> <ul><li><p>队列为普通的队列
<img src="https://upload-images.jianshu.io/upload_images/1846623-278555911de7ece8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="Queue"></p></li> <li><p>回到代码中，定义消息生产者</p></li></ul> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-119d4f2e1081cb0f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="Orderender"></p> <p><em>在消息投递之前为每条消息都设置了延迟时长<code>setDelay()</code>。</em> <em>调用消费者的代码在上面<code>OrderController</code>中，下定之后，订单数据落库，并且向MQ中投递延迟消息。可以回头看看。</em></p> <ul><li>消费者-监听过期的订单信息，并且将DB中相应的订单设置为已过期。</li></ul> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-b4c66c9f37a49b84.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="OrderConsumer"></p> <p><em>为了方便查看到延迟投递的效果，我在消息投递和接收处都打印了日志，测试时可以看到消息投递和消息的时间间隔。</em></p> <h3 id="测试"><a href="#测试" class="header-anchor">#</a> # 测试</h3> <ul><li>把订单过期时长设置为10S</li></ul> <div class="language-yml line-numbers-mode"><pre class="language-yml"><code><span class="token key atrule">app</span><span class="token punctuation">:</span>
  <span class="token key atrule">rabbitmq</span><span class="token punctuation">:</span>
    <span class="token key atrule">delay</span><span class="token punctuation">:</span>
      <span class="token key atrule">order</span><span class="token punctuation">:</span> 10S
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ul><li>下定</li></ul> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-fa5b1f650cf06878.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="log"></p> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-eefd519cfac2916e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="DB100"></p> <p><em>可以看到，打印出了投递日志，订单主键为<code>666ae86aabe2a1b3120b34bb5f447bbe</code>的订单在<code>2020-04-14 22:22:04.307</code>进行了投递，此时数据库中该订单的状态为<code>100</code>，待支付。</em></p> <ul><li>此时查看Exchange详情可以发现，<code>messages delayed:1</code>，即目前有一条消息处于延迟状态。</li></ul> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-ea03e7ca59ae2bad.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="ExchangeDetail"></p> <ul><li>等待10S后。</li></ul> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-833b54515f597bb8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="log"></p> <p><em>可以看到<code>OrderConsumer</code>在10S后<code>2020-04-14 22:22:14.320</code>接收到了主键为<code>666ae86aabe2a1b3120b34bb5f447bbe</code>的订单消息</em>
。距离投递时间<code>2020-04-14 22:22:04.307</code>为10S。此时查看DB中订单状态:</p> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-0ebd0a0b37d19408.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="DB"></p> <p><em>订单状态为<code>200</code>已过期，且过期时间为<code>2020-04-14 22:22:14</code></em></p> <ul><li>达到了订单在我们指定的时间后过期。</li></ul> <hr> <ul><li>再测试几条一分钟的场景</li></ul> <div class="language-yml line-numbers-mode"><pre class="language-yml"><code><span class="token key atrule">app</span><span class="token punctuation">:</span>
  <span class="token key atrule">rabbitmq</span><span class="token punctuation">:</span>
    <span class="token key atrule">delay</span><span class="token punctuation">:</span>
      <span class="token key atrule">order</span><span class="token punctuation">:</span> 1M
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><img src="https://upload-images.jianshu.io/upload_images/1846623-b6ba2f8c9ba369b9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="Exchange"></p> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-229bf1031d3c5f32.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="log"></p> <p><em>消息都在延迟1分钟后投递到了队列-消费者。</em></p> <p>建议收藏，当然我只是建议。</p> <hr> <hr> <h3 id="严重风险提示"><a href="#严重风险提示" class="header-anchor">#</a> # 严重风险提示：</h3> <p><strong>在实际业务使用中，如果消费者的消费能力比较低下，会存在已经过期的消息阻塞积压在队列，无法在指定的时间内过期，导致业务出现异常。
实际上，按照我们业务意图，队里Queue里是不应该有大量消息存在的，因为投递到过期队列的消息已经是过期了的，应该立即被消费掉。</strong></p> <ul><li>进行测试:
<em>为了降低消费者的消费能力，进行如下处理：</em> <ol><li>设置消费者的最大并发数为1，并进行手动签收。</li></ol></li></ul> <div class="language-yml line-numbers-mode"><pre class="language-yml"><code>    <span class="token key atrule">listener</span><span class="token punctuation">:</span>
      <span class="token key atrule">simple</span><span class="token punctuation">:</span>
        <span class="token comment"># 每个队列启动的消费者数量</span>
        <span class="token key atrule">concurrency</span><span class="token punctuation">:</span> <span class="token number">1</span>
        <span class="token comment"># 每个队列最大的消费者数量</span>
        <span class="token key atrule">max-concurrency</span><span class="token punctuation">:</span> <span class="token number">1</span>
        <span class="token key atrule">acknowledge-mode</span><span class="token punctuation">:</span> manual

    <span class="token key atrule">app</span><span class="token punctuation">:</span>
      <span class="token key atrule">rabbitmq</span><span class="token punctuation">:</span>
        <span class="token key atrule">delay</span><span class="token punctuation">:</span>
          <span class="token comment"># 订单过期时间为1分钟</span>
          <span class="token key atrule">order</span><span class="token punctuation">:</span> 1M
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><ol start="2"><li><p>消费者在处理消息时休眠5S
<img src="https://upload-images.jianshu.io/upload_images/1846623-197a45bd5345d81e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="sleep"></p></li> <li><p>向MQ投递两条消息，预期两条消息都在1分钟后正常过期。</p></li> <li><p>结果(去除了无关信息)：</p></li></ol> <p><img src="https://upload-images.jianshu.io/upload_images/1846623-9fe74d7c2310990e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="result"></p> <div class="language-log line-numbers-mode"><pre class="language-text"><code>2020-04-15 20:18:05.269  OrderSender       : 订单[d6fd965b11f8db0fafb762d305db83b0]投递到MQ
2020-04-15 20:18:05.765  OrderSender       : 订单[77ceb7f1bfbbcaf627224ac75e96b0e5]投递到MQ

2020-04-15 20:19:05.279  OrderConsumer     : 消费者接收到延迟订单[d6fd965b11f8db0fafb762d305db83b0]
2020-04-15 20:19:15.316  OrderConsumer     : 订单业务处理结束.....进行消息ack签收

2020-04-15 20:19:15.318  OrderConsumer     : 消费者接收到延迟订单[77ceb7f1bfbbcaf627224ac75e96b0e5]
2020-04-15 20:19:25.330  OrderConsumer     : 订单业务处理结束.....进行消息ack签收
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><em>第一个订单<code>d6fd965b11f8db0fafb762d305db83b0</code>投递时间为<code>2020-04-15 20:18:05.269</code>。1分钟后<code>2020-04-15 20:19:05.279</code>
接收到了通知，并且处理了10S后进行了签收ack。</em> <em>第二个订单<code>77ceb7f1bfbbcaf627224ac75e96b0e5</code>投递时间为<code>2020-04-15 20:18:05.765</code>
。1分钟过后并没有收到通知，而是在第一个订单处理完毕之后，<code>2020-04-15 20:19:15.318</code>才收到了通知，比预期的时间长了10秒，实际延迟时间为1分钟+10秒。出现了业务异常。</em></p> <ul><li>导致这个问题的原因就是消费者无法及时消费消息并更新订单状态。所以我们在进行开发时，需要考虑实际的数据量大小，消费者消费能力。及时关注队列消息积压情况，灵活调整消费者并发数量，优化消费者代码，提高消费者消费能力。</li></ul> <h3 id="系列文章"><a href="#系列文章" class="header-anchor">#</a> # 系列文章</h3> <ul><li><a href="https://www.jianshu.com/p/432167bbe95f" target="_blank" rel="noopener noreferrer">SpringBoot RabbitMQ实现消息可靠投递<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.jianshu.com/p/be0361e222ac" target="_blank" rel="noopener noreferrer">RabbitMQ死信队列在SpringBoot中的使用<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <p><strong>任何技术的使用都不可生搬硬套，需要结合自己实际的业务场景进行相应的调整优化。在平时的工作中应该多关注程序在实际的运行过程中的结果是否符合我们的预期</strong></p> <p><em>本文涉及的源代码:https://github.com/FutaoSmile/springboot-learn-integration/releases/tag/v_rabbitmq_delay_queue</em></p></div> <footer class="page-edit"><!----> <!----></footer> <!----> </main></div><div class="global-ui"></div></div>
    <script src="/press-blog/assets/js/app.d4168a11.js" defer></script><script src="/press-blog/assets/js/2.8d6ea248.js" defer></script><script src="/press-blog/assets/js/10.f7fbfdbe.js" defer></script>
  </body>
</html>
